package com.witmore.skyline.web.controller;

import com.witmore.skyline.db.entity.CameraPicDO;
import com.witmore.skyline.web.config.GlobalAccountSession;
import com.witmore.skyline.web.controller.enums.WitmoreErrorCode;
import com.witmore.skyline.web.controller.vo.param.CameraBindDeviceParamVO;
import com.witmore.skyline.web.controller.vo.param.CameraListParamVO;
import com.witmore.skyline.web.controller.vo.param.CameraParamVO;
import com.witmore.skyline.web.controller.vo.param.CameraUpdateNameParamVO;
import com.witmore.skyline.web.controller.vo.param.CameraUpdateDeviceSnParamVO;
import com.witmore.skyline.web.controller.vo.result.AccountSession;
import com.witmore.skyline.web.controller.vo.result.CameraResultVO;
import com.witmore.skyline.web.controller.vo.result.ResponsePageResult;
import com.witmore.skyline.web.controller.vo.result.ResponseResult;
import com.witmore.skyline.web.service.CameraCaptureService;
import com.witmore.skyline.web.service.CameraService;
import com.witmore.skyline.web.service.third.tplink.dto.DeviceSDCardInfo;
import com.witmore.skyline.web.service.third.tplink.dto.DeviceVideoDayInfo;
import com.witmore.skyline.web.service.third.tplink.dto.DeviceVideoInfoDTO;
import com.witmore.skyline.web.service.third.yjx.dto.CameraLiveDTO;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 摄像头相关接口2.0
 * Author: 豆子高
 * Date: 2025/2/26 14:26
 */
@RestController
@RequestMapping("api/camera/")
public class CameraController {

    @Autowired
    private CameraService cameraService;

    @Autowired
    private CameraCaptureService cameraCaptureService;

    /**
     * 摄像头直播地址 2.0
     */
    @GetMapping(value = "cameraLiveInfo", name = "摄像头直播地址")
    public ResponseResult<CameraLiveDTO> cameraLiveInfo(@Valid CameraParamVO cameraParamVO) throws Exception {
        return cameraService.getCameraLiveUrl(cameraParamVO);
    }

    /**
     * 摄像头列表（分页）2.0
     */
    @GetMapping(value = "cameraList", name = "摄像头列表（分页）2.0")
    public ResponsePageResult<CameraResultVO> cameraList(CameraListParamVO paramPageVO) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        paramPageVO.setOrgId(accountInfo.getOrgId());
        paramPageVO.setProjectId(accountInfo.getProjectId());
        return cameraService.getCameraList(paramPageVO);
    }

    /**
     * 设备监控视频
     */
    @GetMapping(value = "cameraListByDevice", name = "设备监控视频")
    public ResponseResult<List<CameraResultVO>> cameraListByDevice(@RequestParam("skylineDeviceId") Integer skylineDeviceId,
                                                                   @RequestParam(value = "cameraPicCount", required = false) Integer cameraPicCount) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        // 设置默认值
        if (cameraPicCount == null) {
            cameraPicCount = 4;
        }
        return cameraService.getCameraListByDevice(skylineDeviceId, accountInfo.getOrgId(), cameraPicCount);
    }


    /**
     * 摄像头抓图 2.0
     */
    @GetMapping(value = "capturePhoto", name = "摄像头抓图")
    public ResponseResult<Void> capturePhoto(@RequestParam(value = "cameraSn") String cameraSn) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        return cameraCaptureService.cameraCaptureTask(accountInfo, cameraSn);
    }

    /**
     * 摄像头抓图列表 2.0
     */
    @GetMapping(value = "picList", name = "摄像头抓图列表")
    public ResponsePageResult<CameraPicDO> cameraPicList(CameraListParamVO paramPageVO) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        paramPageVO.setOrgId(accountInfo.getOrgId());
        return cameraCaptureService.getCameraPicList(paramPageVO);
    }

    /**
     * 保存视频图片 2.0
     */
    @GetMapping(value = "/saveUrl", name = "保存视频图片")
    public ResponseResult<Void> saveCameraUrl(@RequestParam(value = "cameraSn") String cameraSn,
                                              @RequestParam(value = "ossUrl") String ossUrl) {
        AccountSession accountInfo = GlobalAccountSession.getUserInfo();
        return cameraCaptureService.saveCameraUrl(accountInfo.getOrgId(), cameraSn, ossUrl);
    }

    /**
     * 搜索存在回放录像的日期 2.0
     */
    @GetMapping(value = "searchVideoYear", name = "搜索存在回放录像的日期")
    public ResponseResult<List<DeviceVideoInfoDTO>> searchVideoYear(@RequestParam(value = "cameraSnList") String cameraSnList,
                                                                    @RequestParam(value = "year") String year) {
        if (StringUtils.isEmpty(cameraSnList)) {
            return ResponseResult.fail(WitmoreErrorCode.PARAM_ERROR.getCode(),"摄像头SN不能为空");
        }
        // 将逗号隔开字符串转list
        String[] cameraSnArray = cameraSnList.split(",");
        List<String> cameraSns = new java.util.ArrayList<>(List.of(cameraSnArray));
        // 过滤重复的值
        cameraSns = cameraSns.stream().distinct().toList();
        // 过滤为空的值
        cameraSns = cameraSns.stream().filter(s -> !s.isEmpty()).toList();
        if (cameraSns.isEmpty()) {
            return ResponseResult.fail(WitmoreErrorCode.PARAM_ERROR.getCode(),"摄像头SN不能为空");
        }
        return cameraService.searchVideoYear(cameraSns, year);
    }

    /**
     * 搜索当天录像数据 2.0
     */
    @GetMapping(value = "searchVideoDay", name = "搜索当天录像数据")
    public ResponseResult<DeviceVideoDayInfo> searchVideoDay(@RequestParam(value = "cameraSn") String cameraSn,
                                                             @RequestParam(value = "searchDay") String searchDay,
                                                             @RequestParam(value = "startIdx") Integer startIdx,
                                                             @RequestParam(value = "endIdx") Integer endIdx) {
        return cameraService.searchVideoDay(cameraSn, searchDay, startIdx, endIdx);
    }

    /**
     * 搜索当天所有人形侦测录像数据 2.0
     */
    @GetMapping(value = "searchHumanDetectionVideo", name = "搜索当天所有人形侦测录像数据")
    public ResponseResult<DeviceVideoDayInfo> searchHumanDetectionVideo(@RequestParam(value = "cameraSn") String cameraSn,
                                                            @RequestParam(value = "searchDay") String searchDay) {
        return cameraService.searchHumanDetectionVideo(cameraSn, searchDay);
    }

    /**
     * 获取设备SD卡信息 2.0
     */
    @GetMapping(value = "getDeviceSDCardInfo", name = "获取设备SD卡信息")
    public ResponseResult<List<DeviceSDCardInfo>> getDeviceSDCardInfo(@RequestParam(value = "cameraSn") String cameraSn) {
        return cameraService.getDeviceSDCardInfo(cameraSn);
    }

    /**
     * 设备绑定摄像头 2.0
     */
    @PostMapping(value = "bindCameraToDevice", name = "设备绑定摄像头")
    public ResponseResult<Void> bindCameraToDevice(@Valid @RequestBody CameraBindDeviceParamVO paramVO) {
        return cameraService.bindCameraToDevice(paramVO);
    }

    /**
     * 修改摄像头名称 只有摄像头在线才能修改
     */
    @PostMapping(value = "updateCameraName", name = "修改摄像头名称")
    public ResponseResult<Void> updateCameraName(@Valid @RequestBody CameraUpdateNameParamVO paramVO) {
        return cameraService.updateCameraName(paramVO);
    }

    /**
     * 编辑摄像头设备序列号 2.0
     */
    @PostMapping(value = "updateCameraDeviceSn", name = "编辑摄像头设备序列号")
    public ResponseResult<Void> updateCameraDeviceSn(@Valid @RequestBody CameraUpdateDeviceSnParamVO paramVO) {
        String userName = GlobalAccountSession.getUserInfo().getUserName();
        if (StringUtils.isEmpty(userName) || !StringUtils.equals(userName, "admin")) {
            return ResponseResult.fail(500, "非管理员用户不能操作");
        }
        return cameraService.updateCameraDeviceSn(paramVO);
    }
}
